﻿
using _17bangMVC.Models;
using GLB.Global;
using SRV.ServiceInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GLB;

namespace _17bangMVC.Controllers
{
    //session 重定向也返回错误信息
    
    public class RegisterController : Controller
    {
        private IUserService userService;
        public RegisterController(IUserService userService)
        {
            this.userService = userService;
        }

        // GET: Register
        [HttpGet]
        public ActionResult Index()
        {
            UserModel model = new UserModel()
            {
                
            };
            return View();
        }
        [HttpPost]
        public ActionResult Index(UserModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            //if (userRepository.GetByName(model.Name) != null)
            //{
            //    ModelState.AddModelError("Name", "用户名不能重复");
            //    return View(model);
            //}
            int userId = userService.Register(model);
            HttpCookie cookie = new HttpCookie(Keys.User);
            cookie.Values.Add(Keys.Id, userId.ToString());
            cookie.Values.Add(Keys.Password, model.Password.MD5Encrypt());
            cookie.Expires = DateTime.Now.AddDays(14);
            Response.Cookies.Add(cookie);

            return View();
        }
    }
}